#include "DeviceCtrlProperty.h"
#include <sys/stat.h>
#include <stdlib.h>
//#include "compatibility.h"

//#include <io.h>

DAQCtrlPropertyConfig::DAQCtrlPropertyConfig()
{

}

DAQCtrlPropertyConfig::~DAQCtrlPropertyConfig()
{

}

bool DAQCtrlPropertyConfig::GetSpecFromFile(char* pszfilename)
{
    if (NULL != pszfilename)
    {
        m_vProperty_Spec.clear();
        m_vProperty_ReadBack.clear();
        FILE* file = fopen(pszfilename, "rb");
        if (NULL != file)
        {
			struct stat buf;
			const char *filename = pszfilename;
			stat(filename, &buf);
			int size = buf.st_size;
            //int size = filelength(fileno(file));
            char* psz_buffer = new char[size];
            if (NULL != psz_buffer)
            {
                printf("allocate mem ok!\r\n");
                int i_read_count = fread(psz_buffer, sizeof(char), size, file);
                
                printf("The data read back size is %d\r\n", i_read_count);

                if (i_read_count == size)
                {
                    printf("read file ok!\r\n");
                    int i = 0;
                    int i_str_begin = 0, i_str_end = 0, i_line_cnt = 0;
                    char* psz_cur = psz_buffer;
					bool b_numeric = false;
                    vector<char*> v_psz_spec;
                    //CTRLPROPERTY prop_temp = {0};

                    do 
                    {
                        if ((*psz_cur == '\n') || (i == (size)))
                        {
							if (0 != v_psz_spec.size())
							{
                                CTRLPROPERTY prop_temp = {0};
								for (int j = 0; j < v_psz_spec.size(); j++)
								{
									switch(j)
									{
									case 0:
                                        if (NULL != v_psz_spec[j])
                                        {
                                            memcpy(prop_temp.description, v_psz_spec[j], strlen(v_psz_spec[j]));
                                            delete[] v_psz_spec[j];
                                        }
										break;
									case 1:
                                        if (NULL != v_psz_spec[j])
                                        {
                                            memcpy(prop_temp.value, v_psz_spec[j], strlen(v_psz_spec[j]));
                                            delete[] v_psz_spec[j];
                                        }
                                        break;
									default:
										break;
									}
								}
                                FillSpecBlank(&prop_temp);
                                m_vProperty_Spec.push_back(prop_temp);
                                v_psz_spec.clear();
								i_line_cnt++;
							}
                       
                        }
						else
						{
							if ((*psz_cur <= '9' && *psz_cur >= '0') || *psz_cur == '.' 
                                || (*psz_cur <= 'Z' && *psz_cur >= 'A') || (*psz_cur <= 'z' && *psz_cur >= 'a') 
                                || *psz_cur == '_' || *psz_cur == ';' )
							{
								if (!b_numeric)
								{
									i_str_begin = i_str_end = i;
									b_numeric = true;
								}
								else
								{
									i_str_end = i;

								}
							}
							else
							{
								if (b_numeric)
								{
									b_numeric = false;
									char* psz_temp = new char[i_str_end - i_str_begin + 2];
									if (psz_temp)
									{
										memcpy(psz_temp, psz_buffer + i_str_begin, i_str_end - i_str_begin + 1);
										psz_temp[i_str_end - i_str_begin +1] = '\0';
                                        v_psz_spec.push_back(psz_temp);
									}
								}
							}
						}
						
                        i++;
						if (i < size)
						{
							psz_cur++;
						}				
                    } while (i <= size);
		    printf("Read %d property from file\r\n", i_line_cnt);
                }
                delete[] psz_buffer;
            }
        }
        fclose(file);
    }
    return true;
}

void DAQCtrlPropertyConfig::DisplayPropDetail()
{
    for (int i = 0; i <m_vProperty_Spec.size(); ++i)
    {
        printf("%d. %s\t%s\r\n", i + 1, m_vProperty_Spec[i].description, m_vProperty_Spec[i].value);
    }
}

void DAQCtrlPropertyConfig::WriteSpecTofile()
{
    int i_strlength = 0, i = 0;
	int x = m_vProperty_ReadBack.size();
    for (i = 0; i <m_vProperty_ReadBack.size(); ++i)
    {
        int length = strlen(m_vProperty_ReadBack[i].description) + strlen(m_vProperty_ReadBack[i].value) + 4;
        i_strlength += length;
    }
    char* pszwrite = new char[i_strlength + 1];
	pszwrite[0] = '\0';
    for (i = 0; i <m_vProperty_ReadBack.size(); ++i)
    {
        char psatab[] = "\t";
        char pszreturn[] = "\r\n";
        strcat(pszwrite, m_vProperty_ReadBack[i].description);
        strcat(pszwrite, psatab);
        strcat(pszwrite, m_vProperty_ReadBack[i].value);
        strcat(pszwrite, pszreturn);
    }
    pszwrite[i_strlength] = '\0';
    PropertyTransHelper::WriteStringToFile("readback.txt", pszwrite, strlen(pszwrite));
    delete[] pszwrite;

}

void DAQCtrlPropertyConfig::FillSpecBlank(PCTRLPROPERTY pvalue)
{
    PropertyMap propmap = (PropertyMap)Type_PropertyMap::GetPropertyID(pvalue->description);
    switch (propmap)
    {
    case AI_AiCtrl_ChannelCount:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiCtrlBase;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Channel:
        {
            pvalue->iscollection = true;
            pvalue->type = _AnalogInputChannel_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiCtrlBase;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_Resolution:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_DataSize:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_DataMask:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_ChannelCountMax:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_ChannelType:
        {
            pvalue->iscollection = false;
            pvalue->type = _AiChannelType;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_OverallValueRange:
        {
            pvalue->iscollection = false;
            pvalue->type = _BOOL_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_Thermosupported:
        {
            pvalue->iscollection = false;
            pvalue->type = _BOOL_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_ValueRanges:
        {
            pvalue->iscollection = true;
            pvalue->type = _ValueRange;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_BufferedAiSupported:
        {
            pvalue->iscollection = false;
            pvalue->type = _BOOL_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_ChannelStartBase:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_ChannelCountBase:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_ConvertClockSources:
        {
            pvalue->iscollection = true;
            pvalue->type = _SignalDrop;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_ConvertClockRange:
        {
            pvalue->iscollection = false;
            pvalue->type = _MATHINTERVAL_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_SamplingMethod:
        {
            pvalue->iscollection = false;
            pvalue->type = _SamplingMethod;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_BurstScanSupported:
        {
            pvalue->iscollection = false;
            pvalue->type = _BOOL_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_ScanClockRange:
        {
            pvalue->iscollection = false;
            pvalue->type = _MATHINTERVAL_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_ScanClockSources:
        {
            pvalue->iscollection = true;
            pvalue->type = _SignalDrop;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_ScanCountMax:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_TriggerSupported:
        {
            pvalue->iscollection = false;
            pvalue->type = _BOOL_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_TriggerCount:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_TriggerSources:
        {
            pvalue->iscollection = true;
            pvalue->type = _SignalDrop;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_TriggerActions:
        {
            pvalue->iscollection = true;
            pvalue->type = _TriggerAction;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrl_Feature_TriggerDelayRange:
        {
            pvalue->iscollection = false;
            pvalue->type = _MATHINTERVAL_;
            pvalue->index = 0;
            pvalue->writable = false;
            pvalue->ctrltype = _AiFeatures;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_Streaming:
        {
            pvalue->iscollection = false;
            pvalue->type = _BOOL_;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_ConvertClock_Rate:
        {
            pvalue->iscollection = false;
            pvalue->type = _DOUBLE_;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_ConvertClock_Source:
        {
            pvalue->iscollection = false;
            pvalue->type = _SignalDrop;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_ScanClock_Rate:
        {
            pvalue->iscollection = false;
            pvalue->type = _DOUBLE_;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_ScanClock_Source:
        {
            pvalue->iscollection = false;
            pvalue->type = _SignalDrop;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_ScanClock_ScanCount:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_ScanChannel_ChannelStart:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_ScanChannel_ChannelCount:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_ScanChannel_IntervalCount:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_ScanChannel_Samples:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_Trigger_Source:
        {
            pvalue->iscollection = false;
            pvalue->type = _SignalDrop;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_Trigger_Edge:
        {
            pvalue->iscollection = false;
            pvalue->type = _ActiveSignal;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_Trigger_Level:
        {
            pvalue->iscollection = false;
            pvalue->type = _DOUBLE_;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_Trigger_Action:
        {
            pvalue->iscollection = false;
            pvalue->type = _TriggerAction;
            pvalue->index = 0;
            pvalue->writable = true;
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_BufferedAiCtrl_Trigger_DelayCount:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _AiCtrlBase;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrlBase_ScanChannel_ChannelStart:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrlBase_ScanChannel_ChannelCount:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrlBase_ScanChannel_Samples:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiCtrlBase_ScanChannel_IntervalCount:
        {
            pvalue->iscollection = false;
            pvalue->type = _INT_;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _BufferAiCtrl;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AI_AiChannel_Gain:
        {
            pvalue->iscollection = true;
            pvalue->type = _ValueRange;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _AiCtrlBase;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AO_AOChannel_Gain:
        {
            pvalue->iscollection = true;
            pvalue->type = _ValueRange;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _AoCtrlBase;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case AO_AOChannel_InitOutputValue:
        {
            pvalue->iscollection = true;
            pvalue->type = _NotaEnumValue;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _AoCtrlBase;
            //pvalue->value = 0;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case Counter_CntrFeatures_Resolution:
    case Counter_CntrFeatures_Capabilityies:
    case Counter_CntrFeatures_ChannelCountMax:
    case Counter_CntrFeatures_DataSize:
    case Counter_EventCountCtrl_Value:
    case Counter_FreqMeterCtrl_Features_FmMethods:
    case Counter_FreqMeterCtrl_Value:
    case Counter_FreqMeterCtrl_Method:
    case Counter_OneShotCtrl_Features_DelayCountRange:
    case Counter_OneShotCtrl_Features_OneShotEventSupported:
    case Counter_OneShotCtrl_DelayCount:
    case Counter_TimerPulseCtrl_Features_TimerEventSupported:
    case Counter_TimerPulseCtrl_Features_TimerFrequencyRange:
    case Counter_TimerPulseCtrl_Frequency:
    case Counter_PwMeterCtrl_Features_OverflowEventSupported:
    case Counter_PwMeterCtrl_Value:
    case Counter_PwModulatorCtrl_Features_HiPeriodRange:
    case Counter_PwModulatorCtrl_Features_LoPeriodRange:
    case Counter_PwModulatorCtrl_PulseWidth:
    case AO_AoCtrlBase_ChannelCount:
    case AO_AoCtrlBase_Channels:
    case AO_AoCtrlBase_Features_Resolution:
    case AO_AoCtrlBase_Features_DataSize:
    case AO_AoCtrlBase_Features_DataMask:
    case AO_AoCtrlBase_Features_ChannelCountMax:
    case AO_AoCtrlBase_Features_ValueRanges:
    case AO_AoCtrlBase_Features_BufferedAoSupported:
    case DIO_DiCtrlBase_PortCount:
        break;
    case DIO_DiCtrlBase_PortDirection:
        {
            pvalue->iscollection = true;
            pvalue->type = _DioPortDir;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _DiCtrlBase;
            pvalue->id = (PropertyId)-1;
        }
        break;
    case DIO_DiCtrlBase_Features_ChannelCountMax:
    case DIO_DiCtrlBase_Features_PortCount:
    case DIO_DiCtrlBase_Features_PortProgrammable:
    case DIO_DiCtrlBase_Features_PortsType:
    case DIO_DiCtrlBase_Features_DataMask:
    case DIO_DiCtrlBase_Features_NoiseFilterSupported:
    case DIO_DiCtrlBase_Features_DiintSupported:
    case DIO_DiCtrlBase_Features_DiintGateSupported:
    case DIO_DiCtrlBase_Features_DiPmintSupported:
    case DIO_DiCtrlBase_Features_DiCosintSupported:
    case DIO_DoCtrlBase_PortCount:
    case DIO_DoCtrlBase_Portdirection:
    case DIO_DoCtrlBase_Features_ChannelCountMax:
    case DIO_DoCtrlBase_Features_PortCount:
    case DIO_DoCtrlBase_Features_PortProgrammable:
    case DIO_DoCtrlBase_Features_PortsType:
    case DIO_DoCtrlBase_Features_DataMask:
    case AO_AoCtrlBase_ExtRefValueForUnipolar:
    case AO_AoCtrlBase_ExtRefValueForBipolar:
    case AO_AoCtrlBase_Features_ExternalRefAntiPolar:
    case AO_AoCtrlBase_Features_ExternalRefRange:
    case Counter_FreqMeterCtrl_CollectionPeriod:
        break;
    case Counter_EventCount_ClockPolarity:
        {
            pvalue->iscollection = true;
            pvalue->type = _SignalPolarity;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_EcClkPolarityOfCounters;
        }
        break;
    case Counter_EventCount_GateEnable:
        {
            pvalue->iscollection = true;
            pvalue->type = _BOOL;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_EcGateEnabledOfCounters;
        }
        break;
    case CFG_EcGatePolarityOfCounters:
        {
            pvalue->iscollection = true;
            pvalue->type = _SignalPolarity;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_EcClkPolarityOfCounters;
        }
	case Counter_EventCount_GatePolarity:
		{
			pvalue->iscollection = true;
            pvalue->type = _SignalPolarity;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_EcGatePolarityOfCounters;
		}
        break;
	case Counter_OneShot_ClkSource:
		{
			pvalue->iscollection = true;
            pvalue->type = _SignalDrop;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_OsClkSourceOfCounters;
		}
        break;
	case Counter_OneShot_ClkPolarity:
		{
			pvalue->iscollection = true;
            pvalue->type = _SignalPolarity;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_OsClkPolarityOfCounters;
		}
        break;
	case Counter_OneShot_GateSource:
		{
			pvalue->iscollection = true;
            pvalue->type = _SignalDrop;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_OsGateSourceOfCounters;
		}
        break;
	case Counter_OneShot_GatePolarity:
		{
			pvalue->iscollection = true;
            pvalue->type = _SignalPolarity;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_OsGatePolarityOfCounters;
		}
        break;
	case Counter_OneShot_OutSignal:
		{
			pvalue->iscollection = true;
            pvalue->type = _OutSignalType;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_OsOutSignalOfCounters;
		}
		break;
	case Counter_OneShot_DelayCount:
		{
			pvalue->iscollection = true;
            pvalue->type = _NotaEnumValue;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_OsDelayCountOfCounters;
		}
        break;
	case Counter_TimerPulse_GateEnable:
		{
			pvalue->iscollection = true;
            pvalue->type = _BOOL;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_TmrGateEnabledOfCounters;
		}
        break;
	case Counter_TimerPulse_GatePolarity:
		{
			pvalue->iscollection = true;
            pvalue->type = _SignalPolarity;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_TmrGatePolarityOfCounters;
		}
        break;
	case Counter_TimerPulse_OutSignal:
		{
			pvalue->iscollection = true;
            pvalue->type = _OutSignalType;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_TmrOutSignalOfCounters;
		}
        break;
	case Counter_TimerPulse_TimerFrequency:
		{
			pvalue->iscollection = true;
            pvalue->type = _NotaEnumValue;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_TmrFrequencyOfCounters;
		}
        break;
	case Counter_FreqMeasure_Method:
		{
			pvalue->iscollection = true;
            pvalue->type = _FreqMeasureMethod;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_FmMethodOfCounters;
		}
        break;
	case Counter_FreqMeasure_MeasurePeriod:
		{
			pvalue->iscollection = true;
            pvalue->type = _NotaEnumValue;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_FmCollectionPeriodOfCounters;
		}
        break;
	case Counter_PulseWidth_GateEnable:
		{
			pvalue->iscollection = true;
            pvalue->type = _BOOL;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_PoGateEnabledOfCounters;
		}
        break;
	case Counter_PulseWidth_GatePolarity:
		{
			pvalue->iscollection = true;
            pvalue->type = _SignalPolarity;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_PoGatePolarityOfCounters;
		}
        break;
	case Counter_PulseWidth_HighPeriod:
		{
			pvalue->iscollection = true;
            pvalue->type = _NotaEnumValue;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_PoHiPeriodOfCounters;
		}
        break;
	case Counter_PulseWidth_LowPeriod:
		{
			pvalue->iscollection = true;
            pvalue->type = _NotaEnumValue;
            pvalue->index = 0;
            pvalue->writable = true;  
            pvalue->ctrltype = _CntrCtrlBase;
            pvalue->id = CFG_PoLoPeriodOfCounters;
		}
		break;
    default:
        break;
    }
}

void DAQCtrlPropertyConfig::SetBufferAIRelatedProperty(BufferedAiCtrl** pctrl)
{ 
    if (m_vProperty_Spec.size() != 0 && *pctrl != NULL)
    {
        for (int i = 0; i < m_vProperty_Spec.size(); ++i)
        {
            CTRLPROPERTY element = m_vProperty_Spec[i];
            CTRLPROPERTY elmt_readback;
            elmt_readback.CopyFrom(&element);
            elmt_readback.value[0] = '\0';
            if (element.ctrltype == _BufferAiCtrl || element.ctrltype == _AiCtrlBase)
            {
                BufferedAiCtrl* bfdAiCtrl = *pctrl;
                int maptype = Type_PropertyMap::GetPropertyID(element.description);
                switch (maptype)
                {
                case AI_BufferedAiCtrl_Streaming:
                    {
                        bool bvalue = PropertyTransHelper::GetBoolValueFromString(element.value);
                        bfdAiCtrl->setStreaming(bvalue);
                        bvalue = bfdAiCtrl->getStreaming();
                        if (bvalue)
                        {
                            sprintf(&elmt_readback.value[0], "%s", "TRUE");
                        }
                        else
                        {
                            sprintf(&elmt_readback.value[0], "%s", "FALSE");
                        }
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_BufferedAiCtrl_ConvertClock_Rate:
                    {
                        double lfvalue = PropertyTransHelper::GetDoubleValueFromString(element.value);
                        ConvertClock* pclock = bfdAiCtrl->getConvertClock();
                        pclock->setRate(lfvalue);
                        ///////////////////////////////////////
                        lfvalue = pclock->getRate();
                        sprintf(&elmt_readback.value[0], "%lf", lfvalue);
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_BufferedAiCtrl_ConvertClock_Source:
                    {
                        int ivalue = PropertyTransHelper::GetSpecEnumValueFromString(element.type, element.value);
                        ConvertClock* pclock = bfdAiCtrl->getConvertClock();
                        pclock->setSource((SignalDrop)ivalue);
                        //////////////////////////////////////
                        ivalue = pclock->getSource();
                        char *psz = PropertyTransHelper::GetSpecEnumStringFromValue(elmt_readback.type, ivalue);
                        memcpy(&elmt_readback.value[0], psz, strlen(psz));
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_AiChannel_Gain:
                    {
                        int channelcount = bfdAiCtrl->getChannelCount();
                        int* pgainlist = new int[channelcount];
                        int j = 0;
                        PropertyTransHelper::GetIntArrayValueFromString(element.value, element.type, pgainlist, channelcount);
                        AiChannelCollection* pchannels = bfdAiCtrl->getChannels();
                        for ( j = 0; j < pchannels->getCount(); ++j)
                        {
                            (pchannels->getItem(j)).setValueRange((ValueRange)pgainlist[j]);
                        }

                        /////////////////////////////////////
                        char psztemp[64] = {0};
                        for (j = 0; j < pchannels->getCount(); ++j)
                        {
                            pgainlist[j] = (pchannels->getItem(j)).getValueRange();
                            char* psz = PropertyTransHelper::GetSpecEnumStringFromValue(elmt_readback.type, pgainlist[j]);
                            sprintf(psztemp, "%s;", psz); 
                            strcat(elmt_readback.value, psztemp);
                        }
                        delete[] pgainlist;
                        m_vProperty_ReadBack.push_back(elmt_readback);

                    }
                    break;
                case AI_BufferedAiCtrl_ScanClock_Rate:
                    {
                        double lfvalue = PropertyTransHelper::GetDoubleValueFromString(element.value);
                        ScanClock* pclock = bfdAiCtrl->getScanClock();
                        pclock->setRate(lfvalue);
                        /////////////////////////////
                        lfvalue = pclock->getRate();
                        sprintf(&elmt_readback.value[0], "%lf", lfvalue);
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_BufferedAiCtrl_ScanClock_Source:
                    {
                        int ivalue = PropertyTransHelper::GetSpecEnumValueFromString(element.type, element.value);
                        ScanClock* pclock = bfdAiCtrl->getScanClock();
                        pclock->setSource((SignalDrop)ivalue);
                        //////////////////////////////////////
                        ivalue = pclock->getSource();
                        char *psz = PropertyTransHelper::GetSpecEnumStringFromValue(elmt_readback.type, ivalue);
                        memcpy(&elmt_readback.value[0], psz, strlen(psz));
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_BufferedAiCtrl_ScanClock_ScanCount:
                    {
                        int ivalue = PropertyTransHelper::GetIntValueFromString(element.type, element.value);
                        ScanClock* pclock = bfdAiCtrl->getScanClock();
                        pclock->setScanCount(ivalue);
                        ////////////////////////////////////
                        ivalue = pclock->getScanCount();
                        sprintf(&elmt_readback.value[0], "%d", ivalue);
                        m_vProperty_ReadBack.push_back(elmt_readback);

                    }
                    break;
                case AI_BufferedAiCtrl_ScanChannel_ChannelStart:
                    {
                        int ivalue = PropertyTransHelper::GetIntValueFromString(element.type, element.value);
                        bfdAiCtrl->getScanChannel()->setChannelStart(ivalue);
                        ////////////////////////////////////////
                        ivalue = bfdAiCtrl->getScanChannel()->getChannelStart();
                        sprintf(&elmt_readback.value[0], "%d", ivalue);
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_BufferedAiCtrl_ScanChannel_ChannelCount:
                    {
                        int ivalue = PropertyTransHelper::GetIntValueFromString(element.type, element.value);
                        bfdAiCtrl->getScanChannel()->setChannelCount(ivalue);
                        //////////////////////////////////////
                        ivalue = bfdAiCtrl->getScanChannel()->getChannelCount();
                        sprintf(&elmt_readback.value[0], "%d", ivalue);
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_BufferedAiCtrl_ScanChannel_IntervalCount:
                    {
                        int ivalue = PropertyTransHelper::GetIntValueFromString(element.type, element.value);
                        bfdAiCtrl->getScanChannel()->setIntervalCount(ivalue);
                        //////////////////////////////////////
                        ivalue = bfdAiCtrl->getScanChannel()->getIntervalCount();
                        sprintf(&elmt_readback.value[0], "%d", ivalue);
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_BufferedAiCtrl_ScanChannel_Samples:
                    {
                        int ivalue = PropertyTransHelper::GetIntValueFromString(element.type, element.value);
                        bfdAiCtrl->getScanChannel()->setSamples(ivalue);
                        //////////////////////////////////////
                        ivalue = bfdAiCtrl->getScanChannel()->getSamples();
                        sprintf(&elmt_readback.value[0], "%d", ivalue);
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_BufferedAiCtrl_Trigger_Source:
                    {
                        int ivalue = PropertyTransHelper::GetSpecEnumValueFromString(element.type, element.value);
                        bfdAiCtrl->getTrigger()->setSource((SignalDrop)ivalue);
                        //////////////////////////////////////
                        ivalue = bfdAiCtrl->getTrigger()->getSource();
                        char *psz = PropertyTransHelper::GetSpecEnumStringFromValue(elmt_readback.type, ivalue);
                        memcpy(&elmt_readback.value[0], psz, strlen(psz));
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_BufferedAiCtrl_Trigger_Edge:
                    {
                        int ivalue = PropertyTransHelper::GetSpecEnumValueFromString(element.type, element.value);
                        bfdAiCtrl->getTrigger()->setEdge((ActiveSignal)ivalue);
                        //////////////////////////////////////
                        ivalue = bfdAiCtrl->getTrigger()->getEdge();
                        char *psz = PropertyTransHelper::GetSpecEnumStringFromValue(elmt_readback.type, ivalue);
                        memcpy(&elmt_readback.value[0], psz, strlen(psz));
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_BufferedAiCtrl_Trigger_Level:
                    {
                        double lfvalue = PropertyTransHelper::GetDoubleValueFromString(element.value);
                        bfdAiCtrl->getTrigger()->setLevel(lfvalue);
                        ///////////////////////////////////////
                        lfvalue = bfdAiCtrl->getTrigger()->getLevel();
                        sprintf(&elmt_readback.value[0], "%lf", lfvalue);
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_BufferedAiCtrl_Trigger_Action:
                    {
                        int ivalue = PropertyTransHelper::GetSpecEnumValueFromString(element.type, element.value);
                        bfdAiCtrl->getTrigger()->setAction((TriggerAction)ivalue);
                        //////////////////////////////////////
                        ivalue = bfdAiCtrl->getTrigger()->getAction();
                        char *psz = PropertyTransHelper::GetSpecEnumStringFromValue(elmt_readback.type, ivalue);
                        memcpy(&elmt_readback.value[0], psz, strlen(psz));
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                case AI_BufferedAiCtrl_Trigger_DelayCount:
                    {
                        int ivalue = PropertyTransHelper::GetIntValueFromString(element.type, element.value);
                        bfdAiCtrl->getTrigger()->setDelayCount(ivalue);
                        //////////////////////////////////////
                        ivalue = bfdAiCtrl->getTrigger()->getDelayCount();
                        sprintf(&elmt_readback.value[0], "%d", ivalue);
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                default:
                    break;
                }
            }
        }
		WriteSpecTofile();
    }
}

void DAQCtrlPropertyConfig::SetInstantAIRelatedProperty(InstantAiCtrl** pctrl)
{
    if (m_vProperty_Spec.size() != 0 && *pctrl != NULL)
    {
        for (int i = 0; i < m_vProperty_Spec.size(); ++i)
        {
            CTRLPROPERTY element = m_vProperty_Spec[i];
            CTRLPROPERTY elmt_readback;
            elmt_readback.CopyFrom(&element);
            elmt_readback.value[0] = '\0';
            if (element.ctrltype == _InstantAiCtrl || element.ctrltype == _AiCtrlBase)
            {
                InstantAiCtrl* IstAiCtrl = *pctrl;
                int maptype = Type_PropertyMap::GetPropertyID(element.description);
                switch (maptype)
                {
                case AI_AiChannel_Gain:
                    {
                        int channelcount = IstAiCtrl->getChannelCount();
                        int* pgainlist = new int[channelcount];
                        int j = 0;
                        PropertyTransHelper::GetIntArrayValueFromString(element.value, element.type, pgainlist, channelcount);
                        AiChannelCollection* pchannels = IstAiCtrl->getChannels();
                        for ( j = 0; j < pchannels->getCount(); ++j)
                        {
                            (pchannels->getItem(j)).setValueRange((ValueRange)pgainlist[j]);
                        }

                        /////////////////////////////////////
                        char psztemp[64] = {0};
                        for (j = 0; j < pchannels->getCount(); ++j)
                        {
                            pgainlist[j] = (pchannels->getItem(j)).getValueRange();
                            char* psz = PropertyTransHelper::GetSpecEnumStringFromValue(elmt_readback.type, pgainlist[j]);
                            sprintf(psztemp, "%s;", psz); 
                            strcat(elmt_readback.value, psztemp);
                        }
                        delete[] pgainlist;
                        m_vProperty_ReadBack.push_back(elmt_readback);

                    }
                    break;
                default:
                    break;
                }
            }
        }
		WriteSpecTofile();
    }
}

void DAQCtrlPropertyConfig::SetBufferAORelatedProperty(BufferedAoCtrl** pctrl)
{
    if (m_vProperty_Spec.size() != 0 && *pctrl != NULL)
    {
        for (int i = 0; i < m_vProperty_Spec.size(); ++i)
        {
            CTRLPROPERTY element = m_vProperty_Spec[i];
            CTRLPROPERTY elmt_readback;
            elmt_readback.CopyFrom(&element);
            elmt_readback.value[0] = '\0';
            if (element.ctrltype == _BufferedAoCtrl || element.ctrltype == _AoCtrlBase)
            {
                BufferedAoCtrl* IstAoCtrl = *pctrl;
                int maptype = Type_PropertyMap::GetPropertyID(element.description);
                switch (maptype)
                {
                case AO_AOChannel_Gain:
                    {
                        int channelcount = IstAoCtrl->getChannelCount();
                        int* pgainlist = new int[channelcount];
                        int j = 0;
                        PropertyTransHelper::GetIntArrayValueFromString(element.value, element.type, pgainlist, channelcount);
                        AoChannelCollection* pchannels = IstAoCtrl->getChannels();
                        for ( j = 0; j < pchannels->getCount(); ++j)
                        {
                            (pchannels->getItem(j)).setValueRange((ValueRange)pgainlist[j]);
                        }
                        
                        /////////////////////////////////////
                        char psztemp[64] = {0};
                        for (j = 0; j < pchannels->getCount(); ++j)
                        {
                            pgainlist[j] = (pchannels->getItem(j)).getValueRange();
                            char* psz = PropertyTransHelper::GetSpecEnumStringFromValue(elmt_readback.type, pgainlist[j]);
                            sprintf(psztemp, "%s;", psz); 
                            strcat(elmt_readback.value, psztemp);
                        }
                        delete[] pgainlist;
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                default:
                    break;
                }
            }
        }
        WriteSpecTofile();
    }
}

void DAQCtrlPropertyConfig::SetInstantAORelatedProperty(InstantAoCtrl** pctrl)
{
    if (m_vProperty_Spec.size() != 0 && *pctrl != NULL)
    {
        for (int i = 0; i < m_vProperty_Spec.size(); ++i)
        {
            CTRLPROPERTY element = m_vProperty_Spec[i];
            CTRLPROPERTY elmt_readback;
            elmt_readback.CopyFrom(&element);
            elmt_readback.value[0] = '\0';
            if (element.ctrltype == _InstantAoCtrl || element.ctrltype == _AoCtrlBase)
            {
                InstantAoCtrl* IstAoCtrl = *pctrl;
                int maptype = Type_PropertyMap::GetPropertyID(element.description);
                switch (maptype)
                {
                case AO_AOChannel_Gain:
                    {
                        int channelcount = IstAoCtrl->getChannelCount();
                        int* pgainlist = new int[channelcount];
                        int j = 0;
                        PropertyTransHelper::GetIntArrayValueFromString(element.value, element.type, pgainlist, channelcount);
                        AoChannelCollection* pchannels = IstAoCtrl->getChannels();
                        for ( j = 0; j < pchannels->getCount(); ++j)
                        {
                            (pchannels->getItem(j)).setValueRange((ValueRange)pgainlist[j]);
                        }
                        
                        /////////////////////////////////////
                        char psztemp[64] = {0};
                        for (j = 0; j < pchannels->getCount(); ++j)
                        {
                            pgainlist[j] = (pchannels->getItem(j)).getValueRange();
                            char* psz = PropertyTransHelper::GetSpecEnumStringFromValue(elmt_readback.type, pgainlist[j]);
                            sprintf(psztemp, "%s;", psz); 
                            strcat(elmt_readback.value, psztemp);
                        }
                        delete[] pgainlist;
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                default:
                    break;
                }
            }
        }
        WriteSpecTofile();
    }
}

void DAQCtrlPropertyConfig::SetInstantDIRelatedProperty(InstantDiCtrl** pctrl)
{
    if (m_vProperty_Spec.size() != 0 && *pctrl != NULL)
    {
        for (int i = 0; i < m_vProperty_Spec.size(); ++i)
        {
            CTRLPROPERTY element = m_vProperty_Spec[i];
            CTRLPROPERTY elmt_readback;
            elmt_readback.CopyFrom(&element);
            elmt_readback.value[0] = '\0';
            if (element.ctrltype == _InstantDiCtrl || element.ctrltype == _DiCtrlBase)
            {
                InstantDiCtrl* IstDiCtrl = *pctrl;
                int maptype = Type_PropertyMap::GetPropertyID(element.description);
                switch (maptype)
                {
                case DIO_DiCtrlBase_PortDirection:
                    {
                        int portcount = IstDiCtrl->getPortCount();
                        int* pdirlist = new int[portcount];
                        int j = 0;
                        PropertyTransHelper::GetIntArrayValueFromString(element.value, element.type, pdirlist, portcount);
                        ICollection<PortDirection>* pports = IstDiCtrl->getPortDirection();
                        for ( j = 0; j < pports->getCount(); ++j)
                        {
                            (pports->getItem(j)).setDirection((DioPortDir)pdirlist[j]);
                        }
                        
                        /////////////////////////////////////
                        char psztemp[64] = {0};
                        for (j = 0; j < pports->getCount(); ++j)
                        {
                            pdirlist[j] = (pports->getItem(j)).getDirection();
                            char* psz = PropertyTransHelper::GetSpecEnumStringFromValue(elmt_readback.type, pdirlist[j]);
                            sprintf(psztemp, "%s;", psz); 
                            strcat(elmt_readback.value, psztemp);
                        }
                        delete[] pdirlist;
                        m_vProperty_ReadBack.push_back(elmt_readback);
                    }
                    break;
                default:
                    break;
                }
            }
        }
        WriteSpecTofile();
    }
}

void DAQCtrlPropertyConfig::SetCounterBaseRelatedProperty(HANDLE hmodule)
{
    if (m_vProperty_Spec.size() != 0 && hmodule != NULL)
    {
        for (int i = 0; i < m_vProperty_Spec.size(); ++i)
        {
            CTRLPROPERTY element = m_vProperty_Spec[i];
            CTRLPROPERTY elmt_readback;
            elmt_readback.CopyFrom(&element);
            elmt_readback.value[0] = '\0';
            if (element.ctrltype == _CntrCtrlBase/* || element.ctrltype == _DiCtrlBase*/)
            {
				int maptype = Type_PropertyMap::GetPropertyID(element.description);
				switch (maptype)
				{
				case Counter_EventCount_ClockPolarity:
				case Counter_EventCount_GateEnable:
				case Counter_EventCount_GatePolarity:
				case Counter_OneShot_ClkSource:
				case Counter_OneShot_ClkPolarity:
				case Counter_OneShot_GateSource:
				case Counter_OneShot_GatePolarity:
				case Counter_OneShot_OutSignal:
				case Counter_OneShot_DelayCount:
				case Counter_TimerPulse_GateEnable:
				case Counter_TimerPulse_GatePolarity:
				case Counter_TimerPulse_OutSignal:
				case Counter_TimerPulse_TimerFrequency:
				case Counter_FreqMeasure_Method:
				case Counter_FreqMeasure_MeasurePeriod:
				case Counter_PulseWidth_GateEnable:
				case Counter_PulseWidth_GatePolarity:
				case Counter_PulseWidth_HighPeriod:
				case Counter_PulseWidth_LowPeriod:
					{
						char* pstr = &element.value[0];
						if (SetPropertyByID(hmodule, element.id, &pstr))
						{
							strcat(elmt_readback.value, pstr);
							m_vProperty_ReadBack.push_back(elmt_readback);
						}	
					}
					break;
				default:
					break;
                }
            }
        }
        WriteSpecTofile();
    }
}

bool DAQCtrlPropertyConfig::SetPropertyByID(HANDLE hmodule, PropertyId id, char** pvalue)
{
    int errorCode = 0;
    char psz_out[4096] = {0};
    char psz_section[128] = {0};
	bool b_success = true;
	char* value = NULL;
	if (pvalue != NULL)
	{
		value = *pvalue;
	}
	else
	{
		b_success = false;
		return b_success;
	}
    try
    {
        //////////////////////////////////////////////////////////
        BIO_DEV_PROPERTY_DETAIL detail = {0};
        int trans_error_code = 0;
        GetPropertyDetail(hmodule, id, &detail, &trans_error_code);
        if (TRANS_SUCCESS != trans_error_code)
        {
            printf("error PropertyID or ProppertyType!\n");
            b_success = false;
			return b_success;
        } 
        DisplayPropertyDetail(detail);
        int i_property_type = detail.datatypeid;
        switch (i_property_type)
        {
        case _INT_:
            {
                int i_data = 0;
                if (_NotaEnumValue != detail.enumtypeid)
                {
                    i_data = PropertyTransHelper::GetSpecEnumValueFromString(detail.enumtypeid, value);
                }
                else
                {
                    i_data = atoi(value);
                }
                errorCode = AdxPropertyWrite(hmodule, id, sizeof(int), (void*)&i_data, 0);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                if (_NotaEnumValue != detail.enumtypeid)
                {
                    sprintf(psz_section, "Set %s Success!\r\nthe value you have set is \r\n%s\r\n", detail.name, PropertyTransHelper::GetSpecEnumStringFromValue(detail.enumtypeid, i_data)); 
                }
                else
                {
                    sprintf(psz_section, "Set %s Success!\r\nthe value you have set is \r\n%d\r\n", detail.name, i_data); 
                }
                
                strcat(psz_out, psz_section);
                errorCode = AdxPropertyRead(hmodule, (Automation::BDaq::PropertyId)id, sizeof(int), &i_data, NULL, NULL);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                if (_NotaEnumValue != detail.enumtypeid)
                {
                    sprintf(psz_section, "the %s readback value is %s\r\n", detail.name, PropertyTransHelper::GetSpecEnumStringFromValue(detail.enumtypeid, i_data));
                    strcat(psz_out, psz_section);
					sprintf(value, "%s", PropertyTransHelper::GetSpecEnumStringFromValue(detail.enumtypeid, i_data));
                }
                else
                {
                    sprintf(psz_section, "the %s readback value is %d\r\n", detail.name, i_data);
                    strcat(psz_out, psz_section);
					sprintf(value, "%d", i_data); 
                }  
                
            }
            break;
        case _LONG_:
        case _ULONG_:
            {
                long l_data = 0;
                if (_NotaEnumValue != detail.enumtypeid)
                {
                    l_data = PropertyTransHelper::GetSpecEnumValueFromString(detail.enumtypeid, value);
                }
                else
                {
                    l_data = atol(value);
                }
                errorCode = AdxPropertyWrite(hmodule, id, sizeof(long), (void*)&l_data, 0);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                if (_NotaEnumValue != detail.enumtypeid)
                {
                    sprintf(psz_section, "Set %s Success!\r\nthe value you have set is \r\n%s\r\n", detail.name, PropertyTransHelper::GetSpecEnumStringFromValue(detail.enumtypeid, l_data)); 
					
                }
                else
                {
                    if (_LONG_ == i_property_type)
                    {
                        sprintf(psz_section, "Set %s Success!\r\nthe value you have set is \r\n%d\r\n", detail.name, l_data);
						

                    }
                    else
                    {
                        sprintf(psz_section, "Set %s Success!\r\nthe value you have set is \r\n0x%X\r\n", detail.name, l_data);
						
                    }
                }
                
                sprintf(psz_section, "Set %s Success!\r\nthe value you have set is \r\n%d\r\n", detail.name, l_data);
                strcat(psz_out, psz_section);
                errorCode = AdxPropertyRead(hmodule, (Automation::BDaq::PropertyId)id, sizeof(long), &l_data, NULL, NULL);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                if (_NotaEnumValue != detail.enumtypeid)
                {
                    sprintf(psz_section, "the %s readback value is %s\r\n", detail.name, PropertyTransHelper::GetSpecEnumStringFromValue(detail.enumtypeid, l_data));
                    strcat(psz_out, psz_section);
					sprintf(value, "%s", PropertyTransHelper::GetSpecEnumStringFromValue(detail.enumtypeid, l_data));
                }
                else
                {
                    if (_LONG_ == i_property_type)
                    {
                        sprintf(psz_section, "the %s readback value is %d\r\n", detail.name, l_data);
						sprintf(value, "%d", l_data);
                    }
                    else
                    {
                        sprintf(psz_section, "the %s readback value is 0x%X\r\n", detail.name, l_data);
						sprintf(value, "0x%X", l_data);
                    }
                    strcat(psz_out, psz_section);
                }  
            }
            break;
        case _DOUBLE_:
            {
                double lf_data = atof(value);
                
                errorCode = AdxPropertyWrite(hmodule, id, sizeof(double), (void*)&lf_data, 0);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }

                sprintf(psz_section, "Set %s Success!\r\nthe value you have set is \r\n%lf\r\n", detail.name, lf_data);
                strcat(psz_out, psz_section);
                errorCode = AdxPropertyRead(hmodule, (Automation::BDaq::PropertyId)id, sizeof(double), &lf_data, NULL, NULL);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                sprintf(psz_section, "the %s readback value is %f\r\n", detail.name, lf_data);
                strcat(psz_out, psz_section);
				sprintf(value, "%lf", lf_data);
            }
            break;
        case _MATHINTERVAL_:
            {
                MathInterval interval_data = {0};
                int mathinterval[2] = {0};
                int length = 2;
                int j = 0;
                PropertyTransHelper::GetIntArrayValueFromString(value, detail.datatypeid, &mathinterval[0], length);
                interval_data.Max = mathinterval[0];
                interval_data.Min = mathinterval[1];
                errorCode = AdxPropertyWrite(hmodule, id, sizeof(MathInterval), (void*)&interval_data, 0);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                sprintf(psz_section, "Set %s Success!\r\nthe value you have set is \r\nMax:%lf, Min:%lf\r\n", 
                    detail.name, interval_data.Max, interval_data.Min);
                strcat(psz_out, psz_section);
                errorCode = AdxPropertyRead(hmodule, (Automation::BDaq::PropertyId)id, sizeof(MathInterval), &interval_data, NULL, NULL);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                sprintf(psz_section, "the %s readback value is\r\nmax: %f\r\nmin: %f\r\n", detail.name, interval_data.Max, interval_data.Min);
                strcat(psz_out, psz_section);
				sprintf(value, "%d;%d", interval_data.Min,interval_data.Max);
            }
            break;
        case _BYTE_ARRAY_:
            {
                int length = detail.length;
                int i = 0;
                unsigned char* p_buf = new unsigned char[detail.length];
                printf("the %s type is byte array, length is %d\r\n", detail.name, length);
                int *pintvalue = new int[length];
                PropertyTransHelper::GetIntArrayValueFromString(value, detail.datatypeid, &pintvalue[0], length);
                for (i = 0; i < length; ++i)
                {
                    p_buf[i] = pintvalue[i];  
                }
                errorCode = AdxPropertyWrite(hmodule, id, length * sizeof(unsigned char), p_buf, 0);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                sprintf(psz_section, "Set %s Success!\r\n", detail.name);
                strcat(psz_out, psz_section);
                errorCode = AdxPropertyRead(hmodule, id, length * sizeof(unsigned char), p_buf, NULL, NULL);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                sprintf(psz_section, "the %s readback value is\r\n", detail.name);
                strcat(psz_out, psz_section);
				value[0] = '\0';
                for (i = 0; i < length / sizeof(unsigned char); ++i)
                {
                    if (_NotaEnumValue != detail.enumtypeid)
                    {
                        sprintf(psz_section, "%s\r\n", PropertyTransHelper::GetSpecEnumStringFromValue(detail.enumtypeid, p_buf[i]));
                        strcat(psz_out, psz_section);
						sprintf(psz_section, "%s;",PropertyTransHelper::GetSpecEnumStringFromValue(detail.enumtypeid, p_buf[i]));
						strcat(value, psz_section);
                    }
                    else
                    {
                        sprintf(psz_section, "%d\r\n", p_buf[i]);
                        strcat(psz_out, psz_section);
						sprintf(psz_section, "%d;", p_buf[i]);
						strcat(value, psz_section);
                    }
                }
                delete[] pintvalue;
                delete[] p_buf;
            }
            break;
        case _LONG_ARRAY_:
        case _ULONG_ARRAY_:
            {
                long length = detail.length;
                int i = 0;
                long* p_buf = new long[length / sizeof(long)];
                int i_length = length / sizeof(long);
                printf("the %s type is LONG/ULONG array, length is %d\r\n", detail.name, length / sizeof(long));
                PropertyTransHelper::GetIntArrayValueFromString(value, detail.enumtypeid, (int*)&p_buf[0], i_length);
                errorCode = AdxPropertyWrite(hmodule, id, length, p_buf, 0);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                sprintf(psz_section, "Set %s Success!\r\n", detail.name);
                strcat(psz_out, psz_section);
                errorCode = AdxPropertyRead(hmodule, id, length, p_buf, NULL, NULL);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                sprintf(psz_section, "the %s readback value is\r\n", detail.name);
                strcat(psz_out, psz_section);
				value[0] = '\0';
                for (i = 0; i < length / sizeof(long); ++i)
                {
                    if (_NotaEnumValue != detail.enumtypeid)
                    {
                        sprintf(psz_section, "%s\r\n", PropertyTransHelper::GetSpecEnumStringFromValue(detail.enumtypeid, p_buf[i]));
                        strcat(psz_out, psz_section);
						sprintf(psz_section, "%s;", PropertyTransHelper::GetSpecEnumStringFromValue(detail.enumtypeid, p_buf[i]));
						strcat(value, psz_section);
                    }
                    else
                    {
                        if (_LONG_ARRAY_ == i_property_type)
                        {
                            sprintf(psz_section, "%d\r\n", p_buf[i]);
							strcat(psz_out, psz_section);
							sprintf(psz_section, "%d;", p_buf[i]);
							strcat(value, psz_section);
                        }
                        else
                        {
                            sprintf(psz_section, "0x%X\r\n", p_buf[i]);
							strcat(psz_out, psz_section);
							sprintf(psz_section, "0x%X;", p_buf[i]);
							strcat(value, psz_section);
                        }
                        
                    }
                    
                }
                delete[] p_buf;
            }
            break;   
        case _DOUBLE_ARRAY_:
            {
                long length = detail.length;
                int i_length = length / sizeof(double);
                int i = 0;
                double* p_buf = new double[length / sizeof(double)];
                printf("the %s type is double array, length is %d\r\n", detail.name, length / sizeof(double));
                PropertyTransHelper::GetDoubleArrayValueFromString(value,  &p_buf[0], i_length);
                errorCode = AdxPropertyWrite(hmodule, id, length, p_buf, 0);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                sprintf(psz_section, "Set %s Success!\r\n", detail.name);
                strcat(psz_out, psz_section);
                errorCode = AdxPropertyRead(hmodule, id, length, p_buf, NULL, NULL);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                sprintf(psz_section, "the %s value is\r\n", detail.name);
                strcat(psz_out, psz_section);
				value[0] = '\0';
                for (i = 0; i < length / sizeof(double); ++i)
                {
                    sprintf(psz_section, "%f\r\n", p_buf[i]);
                    strcat(psz_out, psz_section);
					sprintf(psz_section, "%f;", p_buf[i]);
					strcat(value, psz_section);
                }
                delete[] p_buf;
            }
            break;   
        case _WCHAR_ARRAY_:
            {
                long length = detail.length;
                string st = value;
                wchar_t* p_buf = new wchar_t[length / sizeof(wchar_t) + 1];
                printf("the %s type is WCHAR ARRAY, length is %d\r\n", detail.name, length / sizeof(wchar_t));
                printf("please input the string value:\n");
                int transcnt = mbstowcs(p_buf, value, length / sizeof(wchar_t) + 1);
                errorCode = AdxPropertyWrite(hmodule, id, length, p_buf, 0);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                sprintf(psz_section, "Set %s Success!\r\n", detail.name);
                strcat(psz_out, psz_section);
                errorCode = AdxPropertyRead(hmodule, id, length, p_buf, NULL, NULL);
                if(BioFailed(errorCode))
                {
                    throw errorCode;
                }
                sprintf(psz_section, "the %s readback value is\r\n", detail.name);
                strcat(psz_out, psz_section);
                
                //wchar_t wpsz_section[128];
                //swprintf(&wpsz_section[0], L"%ws\r\n", p_buf); 
		wcstombs(psz_section, p_buf, length / sizeof(wchar_t) + 1);
                //WideCharToMultiByte(CP_ACP, 0, wpsz_section, -1, psz_section, wcslen(wpsz_section) + 1, NULL, NULL);
                strcat(psz_out, psz_section);
				strcpy(value, psz_section);

                delete[] p_buf;
            }
            break;
        default:
            sprintf(psz_section, "error data type!\r\n"); 
            strcat(psz_out, psz_section);
            break;
        }      
    }  
    catch(...)
    {
        // Something is wrong
        if(BioFailed(errorCode))
        {
            sprintf(psz_section, "Some error occurred. And the last error code is Ox%X\r\n", errorCode);
            strcat(psz_out, psz_section);
        }
		b_success = false;
		
    }
    printf(psz_out);
    //PropertyTransHelper::WriteStringToFile("Testlog.txt", psz_out, strlen(psz_out));
    return b_success;
}

void DAQCtrlPropertyConfig::GetPropertyDetail(HANDLE hmodule, int property_id, BIO_DEV_PROPERTY_DETAIL* p_detail, int* p_errorCode)
{
    *p_errorCode = TRANS_SUCCESS;
    InfoTransLastError* p_error = (InfoTransLastError*)p_errorCode;
    int errorCode = 0;
    InfoTransLastError transerr = (InfoTransLastError)*p_errorCode;
    try
    {
        if (PropertyTransHelper::GetPropertyDetail(property_id, *p_detail, transerr))
        {
            AdxPropertyRead(hmodule, (Automation::BDaq::tagPropertyId)property_id, 0, NULL, &p_detail->length, NULL);
            AdxPropertyRead(hmodule, (Automation::BDaq::tagPropertyId)property_id, 0, NULL, NULL, &p_detail->attribute);
        }
    }
    catch(...)
    {
        // Something is wrong
        if(BioFailed(errorCode))
        {
            //printf("Some error occurred. And the last error code is Ox%X\n", errorCode);
            printf("Error!\r\n");
        }
    }
}

void DAQCtrlPropertyConfig::DisplayPropertyDetail(BIO_DEV_PROPERTY_DETAIL detail)
{
    char psz_out[4096] = {0};
    char psz_section[128] = {0};
    char* psz_attribute = Type_PropertyAttribute::GetPropertySpec(detail.attribute);
    sprintf(psz_section, "property:\t%s\r\n", detail.name);
    strcat(psz_out, psz_section);
    sprintf(psz_section, "data length:\t%d\r\n", detail.length);
    strcat(psz_out, psz_section);
    sprintf(psz_section, "accessable:\t%s\r\n", psz_attribute);
    strcat(psz_out, psz_section);
    sprintf(psz_section, "data type:\t%s\r\n", detail.datatypename);
    strcat(psz_out, psz_section);
    
    if (detail.enumtypeid != _NotaEnumValue)
    {
        sprintf(psz_section, "enum type:\t%s\r\n", detail.enumtypename);
        strcat(psz_out, psz_section);
    }
    printf(psz_out);
    PropertyTransHelper::WriteStringToFile("Testlog.txt", psz_out, strlen(psz_out));
}
